﻿@page "/Centre"
@using iOSClub.Data
@using iOSClub.Data.DataModels
@using iOSClub.WebSite.Models
@using Microsoft.EntityFrameworkCore
@inject IDbContextFactory<iOSContext> DbFactory
@inject HttpClient client

<PageTitle>您的iMember中心 - 西建大iOS Club</PageTitle>
<!--suppress ALL -->
<!-- ReSharper disable Html.PathError -->

<GridRow Class="block" Justify="center" Gutter="(20, 24)">
    <GridCol Xs="24" Sm="24" Md="12" Lg="12" Xl="8" Xxl="8">
        <a href="/Centre/PersonalData">
            <Card Hoverable Class="card" Bordered="false">
                @if (Member.Identity != "Founder")
                {
                    <Image Style="height: 100px;width: 100px" Preview="false"
                           Src="@(Member.Gender == "男" ? "Centre/男生.png" : "Centre/女生.png")"/>
                }
                else
                {
                    <Image Style="height: 100px;width: 100px" Preview="false" Src="iOS_Club_LOGO.png"/>
                }
                <Title style="font-size: 36px;">@Member.UserName</Title>
                <Paragraph Type="secondary">ID :@Member.UserId</Paragraph>
                <Paragraph Style="font-size: 16px" Strong>@MemberModel.IdentityDictionary[Member.Identity]</Paragraph>
            </Card>
        </a>
    </GridCol>
    <GridCol Xs="24" Sm="24" Md="12" Lg="12" Xl="16" Xxl="16">
        <AppleCard>
            <TitleTemplate>
                <div style="font-weight: bold;font-size: 30px;line-height: 1.35em;color: #1c1f23">iTool</div>
                <div style="color: #00000073;margin-top: 3px;margin-bottom: 3px">iOS Club出品的小工具</div>
            </TitleTemplate>
            <ContextTemplate>
                @if (Tools.Length == 0)
                {
                    <Empty Image="https://gw.alipayobjects.com/zos/antfincdn/ZHrcdLPrvN/empty.svg"
                           Style="padding: 20px" Description="@("社团还没加入iOS App")"/>
                }
                <Space Size=@(("16", "16")) Wrap>
                    @foreach (var link in Tools)
                    {
                        <SpaceItem>
                            <a href="@link.Url" target="_blank"
                               class="a-btn"
                               title="@link.Description">
                                <div style="text-align: center">
                                    @if (!string.IsNullOrEmpty(link.Icon))
                                    {
                                        if (link.Icon.StartsWith("http"))
                                        {
                                            <img alt="" src="@link.Icon"
                                                 style="height: 40px;width: 40px;border-radius: 4px"/>
                                        }
                                        else
                                        {
                                            <IconFont Style="font-size: 40px" Type="@($"icon-{link.Icon}")"/>
                                        }
                                    }
                                    else
                                    {
                                        <img alt="" src="@link.Url.GetIcon()"
                                             style="height: 40px;width: 40px;border-radius: 4px"/>
                                    }
                                    <div class="btn-description">@link.Name</div>
                                </div>
                            </a>
                        </SpaceItem>
                    }
                </Space>
            </ContextTemplate>
        </AppleCard>
    </GridCol>
    @if (Member.Identity != "Member" && Member.Identity != "Founder")
    {
        <GridCol Xs="24" Sm="24" Md="12" Lg="12" Xl="16" Xxl="16">
            <AppleCard>
                <TitleTemplate>
                    <div style="font-weight: bold;font-size: 30px;line-height: 1.35em;color: #1c1f23">我的任务</div>
                    <div style="color: #00000073;margin-top: 3px;margin-bottom: 3px">iStaff 的任务</div>
                </TitleTemplate>
                <ContextTemplate>
                    @if (Staff == null || Staff.Tasks.Count == 0)
                    {
                        <Empty Image="https://gw.alipayobjects.com/zos/antfincdn/ZHrcdLPrvN/empty.svg"
                               Style="padding: 20px" Description="@("您的任务都已经完成了")"/>
                    }
                    else
                    {
                        <AntList DataSource="@Staff.Tasks" TItem="TaskModel">
                            <ListItem>
                                <ListItemMeta>
                                    <TitleTemplate>
                                        <Space Align="center" Size="@("large")">
                                            <SpaceItem>
                                                <div
                                                    style="font-size: 18px;line-height: 1.35em;color: #1c1f23">@context.Title</div>
                                            </SpaceItem>
                                            <SpaceItem>
                                                <Tag Color="@TaskColor(context)">@TaskString(context)</Tag>
                                            </SpaceItem>
                                        </Space>
                                    </TitleTemplate>
                                    <DescriptionTemplate>
                                        <div
                                            style="color: #00000073;margin-top: 3px;margin-bottom: 3px">@($"{context.StartTime} - {context.EndTime}")</div>
                                    </DescriptionTemplate>
                                </ListItemMeta>
                            </ListItem>
                        </AntList>
                    }
                </ContextTemplate>
            </AppleCard>
        </GridCol>
        <GridCol Xs="24" Sm="24" Md="12" Lg="12" Xl="8" Xxl="8">
            <a href="/Centre/Resource">
                <AppleCard>
                    <TitleTemplate>
                        <div style="font-weight: bold;font-size: 30px;line-height: 1.35em;color: #1c1f23">社团资源</div>
                        <div style="color: #00000073;margin-top: 3px;margin-bottom: 3px">iOS Club资源全览</div>
                    </TitleTemplate>
                    <ContextTemplate>
                        @if (Resources.Length == 0)
                        {
                            <Empty Image="https://gw.alipayobjects.com/zos/antfincdn/ZHrcdLPrvN/empty.svg"
                                   Style="padding: 20px"
                                   Description="@("社团现在还没有资源")"/>
                        }
                        else
                        {
                            <AntList DataSource="@Resources" Style="overflow: auto">
                                <ListItem>
                                    <ListItemMeta Title="@context.Name">
                                        <DescriptionTemplate>
                                            <Text Type="secondary">@context.Description</Text>
                                        </DescriptionTemplate>
                                    </ListItemMeta>
                                </ListItem>
                            </AntList>
                        }
                    </ContextTemplate>
                </AppleCard>
            </a>
        </GridCol>
    }
    @if (Member.Identity.Contains("Minister") || Member.Identity == "President" || Member.Identity == "Founder")
    {
        <GridCol Xs="24" Sm="24" Md="12" Lg="12"
                 Xl="@(Member.Identity != "Founder" ? 8 : 16)"
                 Xxl="@(Member.Identity != "Founder" ? 8 : 16)">
            <a href="/Centre/Department">
                <AppleCard>
                    <TitleTemplate>
                        <div style="font-weight: bold;font-size: 30px;line-height: 1.35em;color: #1c1f23">社团部门</div>
                        <div style="color: #00000073;margin-top: 3px;margin-bottom: 3px">iOS 部门管理</div>
                    </TitleTemplate>
                    <ContextTemplate>
                        <AntList DataSource="@Departments" Style="overflow: auto">
                            <ListItem>
                                <ListItemMeta Title="@context.Name">
                                    <DescriptionTemplate>
                                        <Text Type="secondary">@context.Description</Text>
                                    </DescriptionTemplate>
                                </ListItemMeta>
                            </ListItem>
                        </AntList>
                    </ContextTemplate>
                </AppleCard>
            </a>
        </GridCol>
        <GridCol Xs="24" Sm="24" Md="12" Lg="12"
                 Xl="@(Member.Identity != "Founder" ? 16 : 8)"
                 Xxl="@(Member.Identity != "Founder" ? 16 : 8)">
            <a href="/Admin/MemberData">
                <AppleCard>
                    <TitleTemplate>
                        <div style="font-weight: bold;font-size: 30px;line-height: 1.35em;color: #1c1f23">数据中心</div>
                        <div style="color: #00000073;margin-top: 3px;margin-bottom: 3px">展示社团数据</div>
                    </TitleTemplate>
                    <ContextTemplate>
                        <GridRow Gutter="(16,16)" Style="padding-top: 10px">
                            <GridCol Xs="8" Sm="8" Md="8" Lg="6" Xl="4" Xxl="4">
                                <Statistic GroupSeparator=" " Title="当前成员" Value="@Total" TValue="int"/>
                            </GridCol>
                            <GridCol Xs="8" Sm="8" Md="8" Lg="6" Xl="4" Xxl="4">
                                <Statistic GroupSeparator=" " Title="部员数量" Value="@StaffsCount" TValue="int"/>
                            </GridCol>
                            <GridCol Xs="8" Sm="8" Md="8" Lg="6" Xl="4" Xxl="4">
                                <Statistic GroupSeparator=" " Title="项目数量" Value="@ProjectsCount" TValue="int"/>
                            </GridCol>
                            <GridCol Xs="8" Sm="8" Md="8" Lg="6" Xl="4" Xxl="4">
                                <Statistic GroupSeparator=" " Title="任务数量" Value="@TasksCount" TValue="int"/>
                            </GridCol>
                            <GridCol Xs="8" Sm="8" Md="8" Lg="6" Xl="4" Xxl="4">
                                <Statistic GroupSeparator=" " Title="资源数量" Value="@ResourcesCount" TValue="int"/>
                            </GridCol>
                            <GridCol Xs="8" Sm="8" Md="8" Lg="6" Xl="4" Xxl="4">
                                <Statistic GroupSeparator=" " Title="部门数量" Value="@DepartmentsCount" TValue="int"/>
                            </GridCol>
                            <GridCol Xs="8" Sm="8" Md="8" Lg="6" Xl="4" Xxl="4">
                                <Statistic GroupSeparator=" " Title="待办数量" Value="@TodosCount" TValue="int"/>
                            </GridCol>
                        </GridRow>
                    </ContextTemplate>
                </AppleCard>
            </a>
        </GridCol>
    }
</GridRow>

@code



{
    [CascadingParameter] private MemberModel Member { get; set; } = new();

    private static string TaskColor(TaskModel task)
    {
        var today = DateTime.Today;
        var start = DateTime.Parse(task.StartTime);
        var end = DateTime.Parse(task.EndTime);
        if (today > end) return "red-inverse";
        if (start > today) return "blue-inverse";
        return task.Status ? "#87d068" : "#2db7f5";
    }

    private static string TaskString(TaskModel task)
    {
        var today = DateTime.Today;
        var start = DateTime.Parse(task.StartTime);
        var end = DateTime.Parse(task.EndTime);
        if (today > end) return "超时未完成";
        if (start > today) return "未开始";
        return task.Status ? "已完成" : "正在进行";
    }


    private StaffModel? Staff { get; set; }
    private ResourceModel[] Resources { get; set; } = [];
    private DepartmentModel[] Departments { get; set; } = [];
    private LinkModel[] Tools { get; set; } = [];
    private int Total { get; set; }
    private int ProjectsCount { get; set; }
    private int TasksCount { get; set; }
    private int ResourcesCount { get; set; }
    private int DepartmentsCount { get; set; }
    private int TodosCount { get; set; }
    private int StaffsCount { get; set; }

    protected override async Task OnParametersSetAsync()
    {
        await using var context = await DbFactory.CreateDbContextAsync();
        Staff = await context.Staffs
            .Include(x => x.Tasks)
            .FirstOrDefaultAsync(x => x.UserId == Member.UserId);

        var re = await client.GetAsync("https://link.xauat.site/api/Link/GetCategory/社团出品");
        if (re.IsSuccessStatusCode)
        {
            var data = await re.Content.ReadFromJsonAsync<CategoryModel>();
            Tools = data?.Links.ToArray() ?? [];
        }


        if (Staff == null || Staff.Identity == "Member") return;
        Resources = await context.Resources.ToArrayAsync();
        if (Staff.Identity != "Department")
        {
            Total = await context.Students.CountAsync();
            StaffsCount = await context.Staffs.CountAsync();
            ProjectsCount = await context.Projects.CountAsync();
            TasksCount = await context.Tasks.CountAsync();
            ResourcesCount = await context.Resources.CountAsync();
            DepartmentsCount = await context.Departments.CountAsync();
            TodosCount = await context.Todos.CountAsync();
        }

        Departments = await context.Departments.ToArrayAsync();

        await base.OnParametersSetAsync();
    }
}

<style>

    .Layout-Title {
        color: #f3f5f7 !important;
    }

    .ant-layout {
        background-image: linear-gradient(to bottom right, #ff6b6b, #7171ae, #10bce7);
        overflow: auto;
    }

    .ant-layout-content {
        min-height: 100vh;
    }

    .Layout-Header {
        background: rgba(255, 255, 255, 0.3) !important;
        -webkit-backdrop-filter: blur(20px);
        backdrop-filter: blur(20px);
        box-shadow: inset 0 0 6px rgba(255, 255, 255, 0.3);
        padding-left: 10px;
        padding-right: 10px;
        margin: 10px;
        border-radius: 10px;
    }

    .card {
        border-radius: 20px;
        padding: 10px;
        height: 300px;
        background-color: #fff;
        transition: .2s;
    }

    .card:hover {
        transform: scale(1.02)
    }

    .block {
        padding-left: 10vw;
        padding-right: 10vw;
        margin-top: 45px;
        padding-bottom: 45px;
        margin-left: 0 !important;
        margin-right: 0 !important;
    }

</style>

<style>
    .a-btn {
        display: inline-block;
        transition: .2s;
        cursor: pointer;
        margin: 5px 5px 0 0;
        border-radius: 10px;
        padding: 5px;
        border: 1px solid transparent;
    }

    .a-btn:hover {
        transform: scale(1.02);
        border-color: #1890ff;
    }

    .btn-description {
        color: #00000073;
        text-align: center;
        font-size: 0.8em;
    }

    @@media screen and (max-width: 767px) {
        .a-btn {
            width: 18vw;
        }
    }

    @@media screen and (min-width: 862px) {
        .a-btn {
            width: 6vw;
        }
    }

    @@media screen and (min-width: 768px) {
        .a-btn {
            width: 8vw;
        }
    }

    @@media screen and (min-width: 992px) {
        .a-btn {
            width: initial;
        }
    }
</style>

<script src="//at.alicdn.com/t/c/font_4612528_md4hjwjgcb.js"></script>